﻿using NHunspell;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace SpellChecker
{
    public class NHunSpellCheck : ISpellCheck
    { 
        /// <summary>
        /// 
        /// </summary>
        private static Dictionary<string, string> Lag_Dic = new Dictionary<string, string>();
        private string basePath = string.Empty;
        public NHunSpellCheck()
        {
            basePath = HttpContext.Current.Server.MapPath("/bin/dictionary/");
        }
        static NHunSpellCheck()
        {
            Lag_Dic.Add("en", "en-US");
        }
        
        #region ISpellCheck 成员

        public List<string> GetIncorrectWords(List<string> words, string lang)
        {
            List<string> incorrentWords = new List<string>();

            using (Hunspell hunspell = new Hunspell(Path.Combine(basePath, Lag_Dic[lang] + ".aff"), Path.Combine(basePath, Lag_Dic[lang] + ".dic")))
            {
                foreach (string w in words)
                {
                    bool correct = hunspell.Spell(w);
                    if (!hunspell.Spell(w))
                    {
                        incorrentWords.Add(w);
                    }
                }
            }
            return incorrentWords;
        }

        public List<string> GetSuggest(string word, string lang)
        {
            using (Hunspell hunspell = new Hunspell(Path.Combine(basePath, Lag_Dic[lang] + ".aff"), Path.Combine(basePath, Lag_Dic[lang] + ".dic")))
            {
                return hunspell.Suggest(word);
            }
        }

        #endregion
    }
}
